package org.ilovecode.display {
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import gs.TweenLite;
	
	import org.ilovecode.Utilities;
	import org.ilovecode.buttons.RoundFloorButton;
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Garage extends FloorScreen {
		
		//{ region Stage elements
		
		/*public var garage1Button:SimpleButton;
		public var garage2Button:SimpleButton;
		public var garage3Button:SimpleButton;*/
		public var floorMinus1:MovieClip;
		public var floorMinus2:MovieClip;
		public var floorMinus3:MovieClip;
		public var floorNameTextField:TextField;
		public var backButton:SimpleButton;
		public var downloadPDFButton:SimpleButton;
		public var zoomButton:SimpleButton;
		public var zoomWindow:ZoomWindow;
		public var progressBar:MovieClip;
		
		//} endregion
		
		private const NUM_FLOORS:int = 3;
		private var currentFloor:MovieClip = null;
		private var duration:Number = 0.3;
		private var modeExtension:String = "";
		private var activeFloorNumber:int = -1;
		private var inZoomView:Boolean = false;
		private var selectedMode:String = "";
		
		public function Garage() {
			super(ScreenName.GARAGE, "", "garage");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			
			for (var i:uint = 1; i <= NUM_FLOORS; i++) {
				initTextFloorButton(-i);
			}
			
			floorMinus1.alpha = 1;
			currentFloor = floorMinus1;
			progressBar.visible = false;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
			downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButtonClick);
			zoomButton.addEventListener(MouseEvent.CLICK, zoomButtonClick);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			backButton.removeEventListener(MouseEvent.CLICK, backButtonClick);
			downloadPDFButton.removeEventListener(MouseEvent.CLICK, downloadPDFButtonClick);
			zoomButton.removeEventListener(MouseEvent.CLICK, zoomButtonClick);
		}
		
		override protected function initTextFloorButton(floorNumber:int):void
		{
			var buttonName:String = "floor" + ((floorNumber >= 0) ? "" : "Minus") + Math.abs(floorNumber)  + "Button";
			var refMC:MovieClip = MovieClip(this.getChildByName(buttonName + "Ref"));
			var tfb:RoundFloorButton = new RoundFloorButton(floorNumber, buttonName);
			tfb.x = refMC.x;
			tfb.y = refMC.y;
			this.addChild(tfb);
			_floorButtons.push(tfb);
			
		}
		
		override public function showFloor(floorNumber:int):void {
			activeFloorNumber = floorNumber;
			floorNameTextField.text = "ETAŽA " + floorNumber;
			TweenLite.to(currentFloor, duration, { alpha:0 } );	
			TweenLite.to(MovieClip(this["floorMinus" + Math.abs(floorNumber)]), duration, { alpha:1 } );
			currentFloor = MovieClip(this["floorMinus" + Math.abs(floorNumber)]);
			zoomWindow.clear();
			inZoomView = false;
		}
		
		private function zoomButtonClick(event:MouseEvent):void {
			if (!inZoomView) {
				setAllFloorsToInvisible();
				progressBar.visible = true;
				progressBar.gotoAndPlay(0);
				var url:String = "http://www.centar-bundek.hr/Content/Images/garaza_" + activeFloorNumber + ".png";
				try {
					Utilities.loadContent(url, loadGarageImageComplete);
				} catch (e:Error) {
					trace(e);
				}
			} else {
				zoomWindow.clear();
				TweenLite.to(MovieClip(this["floorMinus" + Math.abs(activeFloorNumber)]), duration, { alpha:1 } );
			}
			inZoomView = !inZoomView;
		}
		
		private function setAllFloorsToInvisible():void {
			for (var i:uint = 1; i <= NUM_FLOORS; i++) {
				MovieClip(this["floorMinus" + i]).alpha = 0;
			}
		}
		
		private function loadGarageImageComplete(event:Event):void {
			progressBar.visible = false;
			var loader:Loader = Loader(event.currentTarget.loader);
			var content:DisplayObject = loader.content;
			zoomWindow.init(content);
			this.zoomWindow.focusRect = false;
			this.stage.focus = zoomWindow;
		}
		
		private function downloadPDFButtonClick(event:MouseEvent):void {
			var url:String = "http://www.centar-bundek.hr/Content/PDF/Garaze/garaza_" + activeFloorNumber + ".pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		private function backButtonClick(event:MouseEvent):void {
			//this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
			controller.showScreen(ScreenName.RENDER);
		}
		
		
		
		
		/*private function addedToStage(event:Event):void {
			init();
		}
		
		public function init():void {
			initListeners();
			floor1.alpha = 1;
			currentFloor = floor1;
			progressBar.visible = false;
		}
		
		private function configUI(mode:String):void {
			setAllFloorsToInvisible();
		}
		
		private function setAllFloorsToInvisible():void {
			for (var i:uint = 1; i <= NUM_FLOORS; i++) {
				MovieClip(this["floor" + i]).alpha = 0;
			}
		}
		
		private function initListeners():void {
			garage1Button.addEventListener(MouseEvent.CLICK, garageButtonClick);
			garage2Button.addEventListener(MouseEvent.CLICK, garageButtonClick);
			garage3Button.addEventListener(MouseEvent.CLICK, garageButtonClick);
			backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
			downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButtonClick);
			zoomButton.addEventListener(MouseEvent.CLICK, zoomButtonClick);
		}
		
		private function zoomButtonClick(event:MouseEvent):void {
			if (!inZoomView) {
				setAllFloorsToInvisible();
				progressBar.visible = true;
				progressBar.gotoAndPlay(0);
				var url:String = "http://www.centar-bundek.hr/Content/Images/garaza_-" + activeFloorNumber + ".png";
				try {
					Utilities.loadContent(url, loadGarageImageComplete);
				} catch (e:Error) {
					trace(e);
				}
			} else {
				zoomWindow.clear();
				TweenLite.to(MovieClip(this["floor" + activeFloorNumber]), duration, { alpha:1 } );
			}
			inZoomView = !inZoomView;
		}
		
		private function loadGarageImageComplete(event:Event):void {
			progressBar.visible = false;
			var loader:Loader = Loader(event.currentTarget.loader);
			var content:DisplayObject = loader.content;
			zoomWindow.init(content);
			this.zoomWindow.focusRect = false;
			this.stage.focus = zoomWindow;
		}
		
		private function downloadPDFButtonClick(event:MouseEvent):void {
			var url:String = "http://www.centar-bundek.hr/Content/PDF/Garaze/garaza_-" + activeFloorNumber + ".pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		private function garageButtonClick(event:MouseEvent):void {
			var name:String = SimpleButton(event.target).name;
			for (var i:uint = 1; i <= NUM_FLOORS; i++) {
				if ("garage" + i + "Button" == name) {
					activeFloorNumber = i;
					floorNameTextField.text = "ETAŽA -" + i;
					TweenLite.to(currentFloor, duration, { alpha:0 } );	
					TweenLite.to(MovieClip(this["floor" + i]), duration, { alpha:1 } );
					currentFloor = MovieClip(this["floor" + i]);
				}
			}
			zoomWindow.clear();
			inZoomView = false;
		}
		
		private function backButtonClick(event:MouseEvent):void {
			this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
		}*/
		
	}

}